package institution

import (
	"feng-che.com/server/driver-quiz/internal/constants"
	serverCommon "fiberCommonUtils/common"
	"fiberCommonUtils/request"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"mongoDB"
	"strings"
)

func SimpleInformationOfInstitutions(c *fiber.Ctx) error {
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	_institutions, err := QueryManyInstitution(ctx, bson.D{
		*mongoDB.CommonFilterNotDeleted(),
	})
	if err != nil {
		return err
	}
	return c.JSON(serverCommon.MakeSuccessJson(_institutions))
}

func SimpleInformationOfVehicle(c *fiber.Ctx) error {
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	result := VehicleList{}
	err := result.FindMany(ctx, &bson.D{})
	if err != nil {
		return err
	}
	return c.JSON(serverCommon.MakeSuccessJson(result))
}

func SimpleInformationOfSchool(c *fiber.Ctx) error {
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	result := &SchoolList{}
	err := result.FindMany(ctx, &bson.D{})
	if err != nil {
		return err
	}
	return c.JSON(serverCommon.MakeSuccessJson(result))
}

func CreateVehicle(c *fiber.Ctx) error {
	type T struct {
		ID      primitive.ObjectID `json:"id"`
		Model   string             `json:"model"`
		Gearbox string             `json:"gearbox" validate:"required"`
		Plate   string             `json:"plate"  validate:"required"`
		Brand   string             `json:"brand"  validate:"required"`
	}
	data := request.ParsePostLikeRequestV2(c, &T{})
	if !data.ID.IsZero() {
		c.Locals("data", data)
		return c.Next()
	}
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	_institution, err := GetCurrentActiveInstitution(ctx)
	if err != nil {
		return fiber.NewError(500, fmt.Sprint("驾校读取错误"))
	}
	_vehicle := new(Vehicle)
	_vehicle.Create()
	_vehicle.InstitutionID = _institution.ID
	_vehicle.VehicleModel = strings.TrimSpace(data.Model)
	_vehicle.CarNumber = strings.ToUpper(strings.TrimSpace(data.Plate))
	_vehicle.VehicleBrand = strings.TrimSpace(data.Brand)
	if data.Gearbox == "1" {
		_vehicle.GearBox = constants.GearBoxManuel
	} else if data.Gearbox == "2" {
		_vehicle.GearBox = constants.GearBoxAuto
	} else {
		return fiber.NewError(500, fmt.Sprint("没有提供正确的变速箱类型，"+data.Gearbox))
	}
	_, err = _vehicle.Save(ctx)
	if err != nil {
		return fiber.NewError(500, "数据保存失败,"+err.Error())
	}
	return c.JSON(serverCommon.MakeSuccessJson("done"))
}

func BindVehicleMachineSN(c *fiber.Ctx) error {
	type T struct {
		ID        primitive.ObjectID `json:"id"`
		MachineSN string             `json:"machineSN"`
	}
	data := request.ParsePostLikeRequestV2(c, &T{})
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	_vehicle := new(Vehicle)
	err := _vehicle.FindByID(ctx, data.ID)
	if err != nil {
		return fiber.NewError(500, fmt.Sprint("车辆读取错误", err.Error()))
	}
	if err := _vehicle.Update(ctx, &bson.D{
		{"$set", bson.D{
			{"machine_sn", data.MachineSN},
		}},
	}); err != nil {
		return fiber.NewError(500, fmt.Sprint("数据保存失败", err.Error()))
	}
	return c.JSON(serverCommon.MakeSuccessJson("done"))
}
